Home /

Developer Resource Kit 5 Article

The Socket Component

What Is the Socket Component?

The Socket component allows you to create a two-way communication link between your sever and another server. Socket connections are often called "raw socket connections" because they work at a lower level than something like the CFHTTP tag and they don't assume any kind of protocol other than what's needed to make and manage the socket connection. In other words, whereas the CFHTTP tag assumes you want to use the hypertext transfer protocol—which implies that request are for a specific page that contains HTTP headers—the Socket component makes no such assumptions. You cannot make a request to a specific document on a server (since requesting a document is usually done through HTTP). Instead you simply create a connection to a specific server on a specific port and then write any kind of data you want, be it an HTTP request, XML document, or instant messaging packet.

Use the Socket component whenever you want to communicate with another server without using HTTP. For instance, you might be working with a messaging protocol that has its own set of headers, or a credit card processing service that expects an XML document written to a secure raw socket connection.

Installing the Socket Component

The Socket component is contained in the net package, so the easiest way to install it is to create the folder structure com/macromedia/net in your web root.

For instance, if your web root is wwwroot, then Socket.cfc should be here:

wwwroot/com/macromedia/net/Socket.cfc

You can also place the component in a folder under a ColdFusion mapping or in a custom tag root.

The Socket component is very simple to install because it is entirely self-contained; you don't have to install any .class or .jar files along with it. That means you can use Socket.cfc in server environments where you do not have access to ColdFusion's class path.

Using the Socket Component

Once you have properly installed the Socket component, the easiest way to work with it is by using CFScript. The following code creates an instance of the Socket component (assume the code is enclosed in CFSCRIPT tags):

socket = createObject("component", "com.macromedia.net.Socket");

The next step is to initialize your new instance of the Socket component. The first argument to the init method is the name of the server you want to connect to, the second argument is the port number, and the final argument is whether you want the connection to be encrypted using SSL or not:

socket.init("someServer.com", 80, false);

Now use either the writeText or writeBytes method to write data to the socket connection and return a response. The example below demonstrates writing an XML document contained in the variable myXmlDoc:

response = socket.writeText(myXmlDoc);

You can use writeText or writeBytes to communicate repeatedly over the same socket connection without creating a new connection each time. When you are finished using the connection, remember to close it:

socket.close();

How Does the Socket Component Work?

The Socket component uses the java.net.Socket object to create and manage socket connections to other computers. It also uses several classes from the java.io (input/output) package to manage the actual transfer of data. Although the underlying Java APIs that work with sockets are a little complicated, the Socket component makes it extremely simple and straightforward to work with socket connections from ColdFusion.